package net.sourceforge.m2settings.model;

import javax.swing.event.TableModelListener;

import org.apache.log4j.Logger;

public class ProxyTableModel implements ExtendableTableModel  {
	private static final long serialVersionUID = 3556670377861360549L;

	public static final int COLUMN_ID = 0;

	public static final int COLUMN_USERNAME = 1;

	public static final int COLUMN_PASSWORD = 2;

	public static final int COLUMN_PROTOCOL = 3;

	public static final int COLUMN_HOST = 4;

	public static final int COLUMN_PORT = 5;

	public static final int COLUMN_NONPROXYHOSTS = 6;

	private static String[] columnTitles;

	private Proxies proxies=null;

	// TODO active

	private Logger log = Logger.getLogger(ProxyTableModel.class);

	public ProxyTableModel(Settings userSettings) {
		log.debug("Create ProxyTableModel with settings: "+userSettings.toString());
		proxies = userSettings.getProxies();
		if (proxies==null) { //no proxies declared in XML
			proxies=new Proxies();
		}
		log.debug("there are "+proxies.getProxyCount()+" proxies");
		columnTitles = new String[7];
		columnTitles[COLUMN_ID] = "id";
		columnTitles[COLUMN_USERNAME] = "username";
		columnTitles[COLUMN_PASSWORD] = "password";
		columnTitles[COLUMN_PROTOCOL] = "protocol";
		columnTitles[COLUMN_HOST] = "host";
		columnTitles[COLUMN_PORT] = "port";
		columnTitles[COLUMN_NONPROXYHOSTS] = "nonProxyHosts";
	}


	public void addTableModelListener(TableModelListener arg0) {
		// TODO Auto-generated method stub

	}

	public Class<?> getColumnClass(int index) {
		if (index==COLUMN_PORT) return Integer.class;
		return String.class;
	}

	public int getColumnCount() {
		return columnTitles.length;
	}

	public String getColumnName(int index) {
		return columnTitles[index];
	}

	public int getRowCount() {
		return proxies.getProxyCount();
	}

	public Object getValueAt(int rowIndex, int columnIndex) {
		Proxy row = proxies.getProxy(rowIndex);
		switch (columnIndex) {
		case 0:
			return row.getId();
		case 1:
			return row.getUsername();
		case 2:
			return row.getPassword();
		case 3:
			return row.getProtocol();
		case 4:
			return row.getHost();
		case 5:
			return row.getPort();
		case 6:
			return row.getNonProxyHosts();

		}
		return null;
	}

	public boolean isCellEditable(int arg0, int arg1) {
		return true;
	}

	public void removeTableModelListener(TableModelListener arg0) {
		// TODO Auto-generated method stub

	}

	public void setValueAt(Object value, int row, int col) {
		Proxy proxy = proxies.getProxy(row);
		switch (col) {
		case COLUMN_HOST:
			assert(String.class==value.getClass());
			proxy.setHost((String)value);
			break;
		case COLUMN_ID:
			assert(String.class==value.getClass());
			proxy.setId((String)value);
			break;
		case COLUMN_NONPROXYHOSTS:
			assert(String.class==value.getClass());
			proxy.setNonProxyHosts((String)value);
			break;
		case COLUMN_PASSWORD:
			assert(String.class==value.getClass());
			proxy.setPassword((String)value);
			break;
		case COLUMN_PORT:
			assert(Integer.class==value.getClass());
			proxy.setPort((Integer)value);
			break;
		case COLUMN_PROTOCOL:
			assert(String.class==value.getClass());
			proxy.setProtocol((String)value);
			break;
		case COLUMN_USERNAME:
			assert(String.class==value.getClass());
			proxy.setUsername((String)value);
			break;
		}
		
	}


	public void addRow() {
		Proxy vProxy=new Proxy();
		proxies.addProxy(vProxy);
	}

}
